var canvas,
	constellationsModel = new ConstellationsModel(),
	constellationsView = new ConstellationsView(),
	shareView = new ShareView(),
	introView = new IntroView(),
	skyModel = new SkyModel(),
	skyView = new SkyView(),
	pandaView = new PandaView(),
	stats, 
	isStarsLoaded = false,
	isIntroSkipped = false,
	bmousedown = false,
	currentId = 0,
	mx = 0,
	my = 0,
	stars = [],
	host = window.location.protocol,
	URLBASE = host +'//'+ window.location.host,
	$body,
	
	self = this;


paper.install(window);

function checkDevice() {
	if (navigator.userAgent.match(/Android/i) ||
		navigator.userAgent.match(/webOS/i) ||
		navigator.userAgent.match(/iPhone/i) ||
		navigator.userAgent.match(/iPad/i) ||
		navigator.userAgent.match(/iPod/i) ||
		navigator.userAgent.match(/BlackBerry/) || 
		navigator.userAgent.match(/Windows Phone/i) || 
		navigator.userAgent.match(/ZuneWP7/i)
		) {
			isMobile = true;
    }
}

$.urlParam = function(name){
	var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
	if (!results) { return 0; }
	return results[1] || 0;
}


function init () {

	// console.log('[MainView] init()')
	canvas = document.getElementById('myCanvas');

	jscolor.init()

	paper.setup(canvas);

	// var st = document.createElement( 'div' );
	// document.body.appendChild( st );
	// stats = new Stats();
	// stats.domElement.style.position = 'absolute';
	// stats.domElement.style.bottom = '0px';
	
	// st.appendChild( stats.domElement );

	//setting up listeners for the models
	constellationsModel.changed.add(constellationsChange)
	skyModel.loaded.add(starsLoaded)

	//setting up listeners for the views
	skyView.changed.add(skyViewChanged)

	//lock intro
	introView.init()
	introView.changed.add(introChanged)
	//skip intro
	// introView.destroy()

	skyModel.init()
	skyView.init()
	

	paper.view.onFrame = function() {
		// stats.update()
	}
	paper.view.onResize = function() {
		// console.log('onResize');
		skyView.render()
	}
	
}

function introChanged(data) {
	// console.log('[Main] introChanged')
	isIntroSkipped = true;
	if(isStarsLoaded) {
		
		setTimeout(function() {
			pandaView.init();
		}, 3000);
		if($.urlParam('stars') == 0) {
			constellationsModel.getConstellations()
		}else {
			constellationsModel.getConstellationById($.urlParam('stars'))
		}
		skyView.show();
	}
}

function starsLoaded(data) {
	// console.log('[Main] skyData Loaded')
	skyView.initStars(data)
}


function skyViewChanged(type) {
	switch (type) {
		case "background_loaded":
			// skyModel.init()
		break;

		case "stars_loaded":
			isStarsLoaded = true;
			if(isIntroSkipped) {
				setTimeout(function() {
					pandaView.init();
				}, 3000);
				if($.urlParam('stars') == 0) {
					constellationsModel.getConstellations()
				}else {
					constellationsModel.getConstellationById($.urlParam('stars'))
				}
				skyView.show();
			}
			
		break;

		case "NAV":
			constellationsView.lock(false)
		break;

		case "SELECTION":
			constellationsView.lock(true)
		break;
		
	}
}

function constellationsChange(type, data) {
	switch (type) {
		case "sent":
			// console.log("[Main] constellation data sent", data)
			shareView.init(data)
			skyView.showShare();

		break;

		case "received":
			// console.log("[Main] constellation data received", data)
			for (var i = 0; i < data.length; i++) {
				if(currentId != data[i].id) {
					var delay = data.length==1 ? 0 : random(5, 30)
					constellationsView.add(data[i], delay)
				}
					
			};
	
		break;

		case "received_unique":
			// console.log("[Main] unique constellation data received", data)
			constellationsView.add(data, 3)
			currentId = data.id
			var id = data.stars[0]
 			var star = project.activeLayer.children[id]
			// console.log('star', star)
			setTimeout(function() {
				skyView.moveSky(star.position.x,star.position.y, 4)
			}, 3000)

			setTimeout(function() {
				constellationsModel.getConstellations();
			},6000)

			shareView.init(data)
			
		break;
	}

}




$(window).ready(function() {
    init();
    $body = $('body');
    // shareView.init()
});


function random(from,to){
	return (Math.random()*(to-from+1)+from);
}